package day4;

/**
 * 快乐数
 *
 * @author wileda
 * @date 2022/12/31  20:07
 */
public class Solution {
    public boolean isHappy(int n) {
        int fast = n;
        int slow = n;
        while (true) {
            slow = getNext(slow);
            fast = getNext(getNext(fast));
            if (slow == fast) {
                break;
            }
        }

        if (fast == 1) {
            return true;
        } else {
            return false;
        }
    }

    public int getNext(int n) {
        int result = 0;
        while (n > 0) {
            int a = n % 10;
            n = n / 10;
            result += a * a;
        }
        return result;
    }


    /**
     * 丑数
     */

    public boolean isUgly(int n) {
        if(n < 1){
            return false;
        }
        while(n % 5 == 0){
            n /= 5;
        }
        while(n % 3 == 0){
            n /= 3;
        }
        while(n % 2 == 0){
            n /= 2;
        }
        return n == 1;
    }
}
